﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

using FRL.DebtChased.WebServices;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Extensions to the ClientInstructionBatch model
	/// </summary>
	public partial class ClientInstructionBatch
	{

		/////////////////////////////////////////////////////////////
		// Static Operations

		public static Dictionary<int, string> GetDictionary(DebtChasedEntities ent)
		{

			// Load the clients
			var batchList = from cib in ent.ClientInstructionBatches
							orderby cib.Name
							select new { cib.ClientInstructionBatchId, cib.Name };

			// Create dictionary
			return batchList.ToDictionary(cib => cib.ClientInstructionBatchId, cib => cib.Name);

		}

		public static List<ClientInstructionBatchTransport> GetTransportList( DebtChasedEntities ent, Client client )
		{

			// Load the client batches
			var batchList = from cib in ent.ClientInstructionBatches
							where cib.ClientId == client.ClientId
							orderby cib.Name
							select new { cib.ClientInstructionBatchId, cib.Name, InUse = (cib.Accounts.Count>0) };

			// Load the return variable
			List<ClientInstructionBatchTransport> Ftmp = new List<ClientInstructionBatchTransport>();
			foreach (var result in batchList)
				Ftmp.Add(
					new ClientInstructionBatchTransport()
					{ 
						ClientInstructionBatchId = result.ClientInstructionBatchId, 
						Name = result.Name, 
						InUse = result.InUse 
					}
				);

			// Return variable
			return Ftmp;

		}

		public static void InterCursorSetFromTransport( 
			DebtChasedEntities ent,
			UserAction.Session session, 
			Client currentClient, 
			ClientTransport clientTransport, 
			int? iUserId
		)
		{

			// Loop through current list
			foreach (ClientInstructionBatch currentBatch in currentClient.ClientInstructionBatches)
			{

				// Find the transport object
				ClientInstructionBatchTransport transport = null;
				foreach (ClientInstructionBatchTransport loop in clientTransport.ClientInstructionBatchList)
				{
					if (loop.ClientInstructionBatchId == currentBatch.ClientInstructionBatchId)
					{
						transport = loop;
						break;
					}
				}

				// If account found...
				if (transport != null)
				{

					// Update the batch
					if (currentBatch.Name != transport.Name)
					{

						// Update the batch
						currentBatch.Name = transport.Name;

						// Update session
						session.AddChildDelta(currentBatch, currentBatch.Name, null);

					}

				}
				else
				{

					// Delete the bank account
					ent.ClientInstructionBatches.DeleteObject(currentBatch);

					// Update session
					session.AddChildDelta(currentBatch, currentBatch.Name, null);

				}

			}

			// Add new batches
			foreach (ClientInstructionBatchTransport transport in clientTransport.ClientInstructionBatchList)
			{

				// Ignore all but new
				if (transport.ClientInstructionBatchId != 0)
					continue;

				// Create a new batch
				ClientInstructionBatch newBatch = ClientInstructionBatch.CreateClientInstructionBatch(0, 0, transport.Name);
				newBatch.Client = currentClient;
				ent.ClientInstructionBatches.AddObject(newBatch);

				// Update session
				session.AddChildDelta(newBatch, newBatch.Name, null);

			}

		}

	}

}
